﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using K9Nano.Utils;

namespace K9Nano.Domain.Entities;

/// <summary>
/// 用户实体类
/// </summary>
public abstract class UserBase: SoftDeleteEntityBase
{
    [StringLength(50)]
    [Column(Order = 100)]
    public string Username { get; set; }

    [StringLength(50)]
    [Column(Order = 110)]
    public string Name { get; set; }

    [StringLength(50)]
    [Column(Order = 120)]
    public string? Phone { get; set; }

    [StringLength(100)]
    [Column(Order = 130)]
    public string? Email { get; set; }

    [StringLength(50)]
    [Column(Order = 140)]
    public string Password { get; set; }

    [StringLength(32)]
    [Column(Order = 150, TypeName = "char(32)")]
    public string Salt { get; set; }

    [Column(Order = 160)]
    public bool IsEnabled { get; set; }

    [Column(Order = 170)]
    public string? AvatarUrl { get; set; }

    [Column(Order = 180)]
    public long? OrganizationId { get; set; }

    public virtual void SetPassword(string password)
    {
        Salt = Guid.NewGuid().ToString("N");
        Password = HashUtils.Md5(Username + password + Salt);
    }

    public virtual bool ValidatePassword(string password)
    {
        var hashed = HashUtils.Md5(Username + password + Salt);
        return Password.Equals(hashed, StringComparison.Ordinal);
    }
}
